package com.qdxwx.manage.controller.advert;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.models.advert.AdvertType;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.advert.AdvertTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.TableDataInfo;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 图片类型Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/advert/type")
public class AdvertTypeController extends BaseController {

    @Autowired
    private AdvertTypeService advertTypeService;

    @GetMapping("/list.html")
    public String list() {
        return "advert/advert-type-list";
    }

    /**
     * 查询图片类型列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        AdvertType advertType = new AdvertType();
        advertType.setTypeName(TKString.queryKey(request.getParameter("typeName")));
        List<AdvertType> list = advertTypeService.getList(advertType, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(advertTypeService.count(advertType));
        return rspData;
    }

    /**
     * 新增保存图片类型
     */
    @Log(title = "图片类型", businessType = BusinessType.INSERT)
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request) {
        String typeName = TKString.queryKey(request.getParameter("typeName"));
        if (TKString.isEmpty(typeName)) return error("请输入广告类型名称");
        AdvertType advertType = new AdvertType();
        advertType.setTypeName(typeName);
        return toAjax(advertTypeService.insert(advertType));
    }


    /**
     * 修改保存图片类型
     */
    @Log(title = "图片类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request) {
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        if (typeId < 1) return error("缺少id");
        String typeName = TKString.queryKey(request.getParameter("typeName"));
        if (TKString.isEmpty(typeName)) return error("请输入广告类型名称");
        AdvertType advertType = new AdvertType();
        advertType.setTypeId(typeId);
        advertType.setTypeName(typeName);
        return toAjax( advertTypeService.update(advertType));
    }

    /**
     * 删除图片类型
     */
    @Log(title = "图片类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if(id < 1) return error("参数异常");
        return toAjax(advertTypeService.delete(id));
    }
}
